requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {

    Constant.initHandlebars(Handlebars);

    var url = {
        list: "manager/selectRights.do",
        addRight: "manager/addRight.do",
        editRight: "manager/updateRight.do"
    }


    //查询
    var frontData = {
        pageNum: 1
    };
    var html = [];

    //全部权限
    var rights = {};

    var id;
    var $this;

    //查询所有权限
    var queryData = function () {
        site.send({
            url: url.list,
            //async:false,
            data: {
                frontData: frontData
            },
            callback: function (data) {
                var data = JSON.parse(data);
                htmlbtn(data.content, '<ul>');
                $('#htmlBody').html(html.join(''));

                $('#htmlBody').treeview({
                    collapsed: true,
                    animated: 'medium',
                    control: '#sidetreecontrol',
                    persist: 'cookie'
                });

                initBtnAction();
            }
        })
    }

    var htmlbtn = function (content) {

        html.push('<ul>');
        $.each(content, function (i, t) {
            rights[t.id] = t;
            t.description = t.name;
            html.push('<li>');
            if (t.children.length != 0) {
                html.push('<div class="tr1" data-type ="true" data-has-child="true" data-focus="false">');
                // html.push('<label>[-]</label>');
            } else {
                html.push('<div class="tr1" data-type ="true" data-has-child="false" data-focus="false">');
                // html.push('<label></label>');
            }

            html.push('<label>' + t.name + '</label>');
            html.push('<label>');
            html.push('<span ng-if="HbHr.hr210" class="second-span _yellow" data-attr="addMenu" data-type="2" data-name="' + t.name + '" data-desciption="' + t.name + '" data-id="' + t.id + '">');
            html.push('添加子菜单');
            html.push('</span>');
            html.push('<span ng-if="HbHr.hr210" class="second-span _yellow" data-attr="addRight" data-type="2" data-name="' + t.name + '" data-desciption="' + t.name + '" data-id="' + t.id + '">');
            html.push('添加权限');
            html.push('</span>');
            html.push('<span ng-if="HbHr.hr211" class="second-span _blue" data-attr="editRight" data-id="' + t.id + '" data-desciption="' + t.name + '" data-pId="' + t.pId + '">');
            html.push('编辑');
            html.push('</span>');
            html.push('<span ng-if="HbHr.hr212" class="second-span _red" data-attr="deleteRight" data-id="' + t.id + '" data-desciption="' + t.name + '" data-name="' + t.name + '">删除</span>');
            html.push('</label>');
            html.push('</div>');
            htmlbtn1(t.children, 'tr11', t.name);

            html.push('</li>');
        });
        html.push('</ul>');
    }
    var htmlbtn1 = function (content, ii, preDes) {
        html.push('<ul>');
        $.each(content, function (i, t) {
            /*
            var rights = {};
            rights[362] = { id:'362' , name :"xxx" };
            rights[12] = { id:'12' , name :"asdf" };
            rights = {
                362:{
                    id:'362'
                    name:'xxx',
                    children:[
                        { id:12 , name }
                    ]
                },
                12:{
                    name:'',
                    url:''
                }
            }*/
            html.push('<li>');

            rights[t.id] = t;
            t.description = preDes + '-' + t.name;
            var description = preDes + '-' + t.name;
            if (t.children.length != 0) {
                html.push('<div class="' + ii + '" data-type ="true" data-has-child="true" data-focus="false">');
                // html.push('<label>[-]</label>');
            } else {
                html.push('<div class="' + ii + '" data-type ="true" data-has-child="false" data-focus="false">');
                // html.push('<label></label>');
            }
            html.push('<label>');
            html.push('<span class="toggle-tu">');
            html.push('<span class="tu-1"></span>');
            html.push('<span class="tu-2"></span>');
            html.push('</label>');
            html.push(t.name);
            html.push('</label>');
            html.push('<label>');
            if (ii != 'tr1111') {
                html.push('<span ng-if="HbHr.hr210" class="second-span _yellow" data-attr="addMenu" data-type="2" data-desciption="' + description + '" data-name="' + t.name + '" data-id="' + t.id + '">');
                html.push('添加子菜单');
                html.push('</span>');
                html.push('<span ng-if="HbHr.hr210" class="second-span _yellow" data-attr="addRight" data-type="2" data-desciption="' + description + '" data-name="' + t.name + '" data-id="' + t.id + '">');
                html.push('添加权限');
                html.push('</span>');
            }
            html.push('<span ng-if="HbHr.hr211" class="second-span _blue" data-attr="editRight" data-id="' + t.id + '" data-name="' + t.name + '" data-pId="' + t.pId + '">');
            html.push('编辑');
            html.push('</span>');
            html.push('<span ng-if="HbHr.hr212" class="second-span _red" data-attr="deleteRight" data-id="' + t.id + '" data-name="' + t.name + '">删除</span>');
            html.push('</label>');
            html.push('</div>');

            if (t.children.length != 0) {
                htmlbtn1(t.children, ii + 1, description);
            }

            html.push('</li>');

        });
        html.push('</ul>');
    };
    //data-attr="addSection"
    //点击添加菜单按钮将内容填充到模板
    $('#addTop').on('click', function () {
        var source = $("#menus_tpl").html();
        var template = Handlebars.compile(source);
        var r = {pId: 0, pName: "顶级菜单"}
        $('#menus').html(template(r));
        $('.menus_winBox').toggle();
        $("#image_upload_pop").hide();
        initWinBoxAciton("addMenu");
    });

    //初始化弹窗里的按钮
    var initWinBoxAciton = function (attr) {
        $("#submit").on('click', function () {
            if (attr == "addMenu") {
                addRight(url.addRight);
            }
            if (attr == "addRight") {
                addRight(url.addRight);
            }
            if (attr == "editRight") {
                addRight(url.editRight);
            }
        })

    }
    var initBtnAction = function () {
        //添加子菜单
        $('span[data-attr=addMenu]').on('click', function () {
            var id = $(this).data('id');
            var name = $(this).data('name');
            var r = {pId: id, pName: name}
            var source = $("#menus_tpl").html();
            var template = Handlebars.compile(source);
            $('#menus').html(template(r));
            $("#type").find("option[value='1']").attr("selected", "selected");
            $('#type').attr("disabled", true);
            $('.menus_winBox').toggle();
            $("#image_upload_pop").hide();

            initWinBoxAciton($(this).data('attr'));
        });
        //添加子权限
        $('span[data-attr=addRight]').on('click', function () {
            var id = $(this).data('id');
            var name = $(this).data('name');
            var r = {pId: id, pName: name}
            var source = $("#menus_tpl").html();
            var template = Handlebars.compile(source);
            $('#menus').html(template(r));
            $("#type").find("option[value='0']").attr("selected", "selected");
            $('#type').attr("disabled", true);
            $('.menus_winBox').toggle();
            $("#image_upload_pop").hide();

            initWinBoxAciton($(this).data('attr'));
        });
        //删除权限
        $('span[data-attr=deleteRight]').on('click', function () {
            var name = $(this).data('name');
            $this = $(this);
            $(".delete_winBox").show();
            //var flag = confirm("您确定要删除" + name + "吗?")
            /*if (flag) {*/
            id = $(this).data('id');
            //}
        })

        $("#sureDelete").unbind().on('click', function () {
            site.send({
                url: "manager/deleteRight.do",
                data: {
                    id: id
                },
                callback: function (data) {
                    data = JSON.parse(data)
                    if (data.type == 1) {
                        Constant.fn.Message('T', data.msg);
                        // $('.winBox').toggle();
                        // $("#image_upload_pop").hide();
                        initWinBoxAciton();
                        $this.parent('label').parent('div').parent('li').remove();
                        $('.delete_winBox').hide();
                        // pjaxReload();
                    }
                    else {
                        Constant.fn.Message('F', data.msg);
                        // $('.winBox').toggle();
                        $("#image_upload_pop").hide();
                    }
                }
            })
        })

        //编辑权限
        $('span[data-attr=editRight]').on('click', function () {
            //获取当前点击对象的id
            var id = $(this).data('id');
            var name = $(this).data('name');
            // var r = { pId:id , pName:name}
            //根据当前id获取当前权限对象
            var r = rights[id];

            if (r.pId == 0) {
                r.pName = "顶级菜单";
            } else {
                r.pName = rights[r.pId].name;
            }
            var source = $("#menus_tpl").html();
            var template = Handlebars.compile(source);
            $('#menus').html(template(r));
            $(".headerSpan").text("编辑菜单");
            $('.menus_winBox').toggle();
            //$("#image_upload_pop").hide();

            initWinBoxAciton($(this).data("attr"));
        })
    }

    function addRight(url) {
        //如果是编辑的话，获取弹窗里面的参数
        var data = {
            name: $('#name').val(),
            description: $("#name").data('description'),
            pId: $('#pId').val(),
            url: $('#url').val(),
            type: $('#type').val(),
            sort: $('#sort').val(),
            id: $('#id').val()
        };

        site.send({
            url: url,
            data: data,
            callback: function (datas) {
                datas = JSON.parse(datas)
                if (datas.type == 1) {
                    if (url == "manager/updateRight.do") {
                        Constant.fn.Message("T", "编辑成功！");
                        $('.menus_winBox').toggle();
                        // pjaxReload();  //刷新当前页面.
                        html = [];
                        queryData();
                    }
                    if (url == "manager/addRight.do") {
                        Constant.fn.Message("T", "新增成功!");
                        $('.menus_winBox').toggle();
                        html = [];
                        queryData();
                        // pjaxReload();  //刷新当前页面.
                    }
                }
                if (datas.type == -1) {
                    if (url == "manager/updateRight.do") {
                        Constant.fn.Message("F", datas.msg);
                    }
                    if (url == "manager/addRight.do") {
                        Constant.fn.Message("F", datas.msg);
                    }
                }
            }
        })
    }

    //进入时搜索
    queryData();
    //搜索
    $("#sousuo").on('click', function () {
        queryData();
    });

    //编辑
    $('#edit').on('click', function () {
        window.location.href = "OperaManager/product/product_edit.html"
    })

    $('#operations').on('change', function () {
        window.location.href = "process/process.html"
    })

    /*$("#operations_record").on('click',function(){
        $('.winBox').toggle().load('loanBefore/common/record.html');
    })*/


    $(".expandable").css({"width": "100%"})


})

